package com.itheima.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.bean.ShoppingCart;
import com.itheima.bean.User;
import com.itheima.dao.ShoppingCartDao;
import com.itheima.service.ShoppingCartService;
import com.itheima.utils.BaseContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;


@Transactional
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Resource
    private ShoppingCartDao shoppingCartDao;

    @Resource
    private HttpSession session;


    @Override
    public ShoppingCart add(ShoppingCart shoppingCart) {

        //1.按着用户的id和菜品或套餐的id去查询购物车表，看看用户有没有这个菜品/套餐
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        //设置查询条件 ：用户ID

        User user = (User) session.getAttribute("user");
        lqw.eq(ShoppingCart::getUserId, user.getId());
        //判断添加的是菜品还是套餐，然后再追加查询条件
        if (shoppingCart.getDishId() != null) {
            //说明把菜品加入购物车
            lqw.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        } else {
            //说明要把套餐加入购物车
            lqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        //执行查询操作后，再看看表里有没有这个用户的菜品/套餐的数据

        ShoppingCart cartInDB = shoppingCartDao.selectOne(lqw);

        //判断数据库还是没有这件商品

        if (cartInDB == null) {
            //如果进入这里，既表示这件商品在表里面没有：要执行添加操作
            shoppingCart.setUserId(user.getId());
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);

            shoppingCartDao.insert(shoppingCart);

            return shoppingCart;
        } else {
            //如果进入这里，说明购物车有这件商品，那就进行更新 加减操作
            cartInDB.setNumber(cartInDB.getNumber() + 1);
            shoppingCartDao.updateById(cartInDB);
            return cartInDB;
        }


    }

    @Override
    public ShoppingCart sub(ShoppingCart shoppingCart) {

        //1.按着用户的id和菜品或套餐的id去查询购物车表，看看用户有没有这个菜品/套餐
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        //设置查询条件 ：用户ID
        User user = (User) session.getAttribute("user");
        lqw.eq(ShoppingCart::getUserId, user.getId());
        //判断减少的是菜品还是套餐，然后再追加查询条件
        if (shoppingCart.getDishId() != null) {
            //说明把菜品加入购物车
            lqw.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        } else {
            //说明要把套餐加入购物车
            lqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        //执行查询操作后，再看看表里有没有这个用户的菜品/套餐的数据

        ShoppingCart cartInDB = shoppingCartDao.selectOne(lqw);

        //判断数据库还是没有这件商品

        if (cartInDB.getNumber() == 1) {
            //如果进入这里，既表示这件商品在表里面没有：要执行添加操作
            //shoppingCart.setUserId(BaseContext.getCurrentId());
            //shoppingCart.setCreateTime(LocalDateTime.now());
            ////shoppingCart.setNumber(0);

            shoppingCartDao.deleteById(cartInDB);
            return shoppingCart;

        } else {
            //如果进入这里，说明购物车有这件商品，那就进行更新 加减操作
            cartInDB.setNumber(cartInDB.getNumber() - 1);


            shoppingCart.setUserId(user.getId());

            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartDao.updateById(cartInDB);
            return cartInDB;
        }


    }

    @Override
    public List<ShoppingCart> findList() {
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        User user = (User) session.getAttribute("user");
        lqw.eq(ShoppingCart::getUserId, user.getId());
        lqw.orderByDesc(ShoppingCart::getCreateTime);
        return shoppingCartDao.selectList(lqw);
    }

    @Override
    public int clean() {
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();

        User user = (User) session.getAttribute("user");
        lqw.eq(ShoppingCart::getUserId, user.getId());

        return shoppingCartDao.delete(lqw);
    }


}
